package com.tangkeyon.common;


import com.tangkeyon.enums.LoginStatusEnum;
import org.codehaus.jackson.map.annotate.JsonSerialize;

/**
 * 用于封装一个通用的返回对象
 */

/**
 * @Author: tangkeyon@gmail.com
 * @Date: 2019/5/21 9:24
 * @Version 1.0
 */

/**
 * 使用该注解表示当实体类中属性的值为空，那么也将该该属性置空，否则前端的某些组件收到有空值的json会报错
 * @param <T>
 */
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class ResponseCode<T> {
    private int status;
    private String desc;
    private T data;

    private ResponseCode(int code,String desc){
        this.status =code;
        this.desc=desc;
    }

    private  ResponseCode(int code,T data){
        this.status =code;
        this.data=data;
    }

    private  ResponseCode(String desc, T data){
        this.desc=desc;
        this.data=data;
    }

    private ResponseCode(int code,String desc,T data){
        this.status =code;
        this.desc=desc;
        this.data=data;
    }

    public int getStatus() {
        return status;
    }

    public String getDesc() {
        return desc;
    }

    public T getData() {
        return data;
    }

    public  boolean ifSuccess(){
      return status==LoginStatusEnum.SUCCESS.getCode();
    }

    public static<T> ResponseCode<T> creatBySuccess(String desc){
        return  new ResponseCode<>(LoginStatusEnum.SUCCESS.getCode(),desc);
    }

    public static<T> ResponseCode<T> creatBySuccessMessage(String desc){
        return  new ResponseCode<>(LoginStatusEnum.SUCCESS.getCode(),desc);
    }

    public static<T> ResponseCode<T> creatBySuccess(T data){
        return new ResponseCode<>(LoginStatusEnum.SUCCESS.getCode(),LoginStatusEnum.SUCCESS.getDesc(),data);
    }

    public static<T> ResponseCode<T>creatBySuccess(T data,String desc){
        return new ResponseCode<>(LoginStatusEnum.SUCCESS.getCode(),desc,data);
    }

    public static<T> ResponseCode<T> createByError(){
        return new ResponseCode<>(LoginStatusEnum.ERROR.getCode(),LoginStatusEnum.ERROR.getDesc());
    }

    public static<T> ResponseCode<T> createByErrorMessage(String desc){
        return new ResponseCode<>(LoginStatusEnum.ERROR.getCode(),desc);
    }

    public static<T> ResponseCode<T> createByErrorCodeMessage(String errorMsg,int code){
        return new ResponseCode<>(code,errorMsg);
    }
}
